import java.io.*;
import java.util.zip.CheckedInputStream;
import java.util.zip.CRC32;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
 
public class Obslugapliku {

	String katalog = new String() ;
	File f ;
	
	Obslugapliku(int port)
	{
		katalog = Integer.toString(port) ;
		katalog += "/" ;
		File dir = new File(Integer.toString(port));
		dir.mkdir();
	}
	
	public int obciazenieSerwera()
	{
		File f = new File(katalog) ;
    	return f.listFiles().length ;
	}
	
	public File[] listaPlikow()
	{
		File f = new File(katalog) ;
    	return f.listFiles() ;
	}
	
    public String czytajLinie(String nazwa) throws IOException
    {
    	
    	RandomAccessFile file = null;
        FileLock fileLock = null;
        String s = new String() ;
        f = new File(katalog+nazwa);
    	if(!f.exists()) { 
    		s = "FILE DONT EXIST"  ;
    		return s ;
    	}
    	
        try
        {
            file = new RandomAccessFile(katalog+nazwa, "rw");
            FileChannel fileChannel = file.getChannel();
 
            fileLock = fileChannel.tryLock();
            if (fileLock != null){
                System.out.println("File is locked");
                s = czytaj(katalog+nazwa);
            }
        }finally{
            if (fileLock != null){
                fileLock.release();
            }
        }
        
        return s ;
    	
    }
    
    
    public String dajSumeKontrolna(String nazwa) throws IOException
    {
    	
    	RandomAccessFile file = null;
        FileLock fileLock = null;
        String s = new String() ;
        f = new File(katalog+nazwa);
    	if(!f.exists()) { 
    		s = "FILE DONT EXIST"  ;
    		return s ;
    	}
    	
        try
        {
            file = new RandomAccessFile(katalog+nazwa, "rw");
            FileChannel fileChannel = file.getChannel();
 
            fileLock = fileChannel.tryLock();
            if (fileLock != null){
                System.out.println("File is locked");
                FileInputStream plik = new FileInputStream(katalog+nazwa);
                CheckedInputStream check = 
                  new CheckedInputStream(plik, new CRC32());
                BufferedInputStream in = 
                  new BufferedInputStream(check);
                while (in.read() != -1) {
                    // Read file in completely
                }
                s = String.valueOf(check.getChecksum().getValue()) ;
            }
        }finally{
            if (fileLock != null){
                fileLock.release();
            }
        }
        
        return s ;
    	
    }
    
    public boolean piszLinie(String nazwa,String data) throws IOException
    {
    	RandomAccessFile file = null;
        FileLock fileLock = null;
        String s = new String() ;
        boolean rezultat = false ;
        try
        {
            file = new RandomAccessFile(katalog+nazwa, "rw");
            FileChannel fileChannel = file.getChannel();
 
            fileLock = fileChannel.tryLock();
            if (fileLock != null){
                System.out.println("File is locked");
                rezultat = pisz(katalog+nazwa,data) ;
            }
        }finally{
            if (fileLock != null){
                fileLock.release();
            }
        }
        return rezultat ;
    	
    }
    
    public boolean usunPlik(String nazwa) throws IOException
    {
    	RandomAccessFile file = null;
        FileLock fileLock = null;
        try
        {
            file = new RandomAccessFile(katalog+nazwa, "rw");
            FileChannel fileChannel = file.getChannel();
 
            fileLock = fileChannel.tryLock();
            if (fileLock != null){
                System.out.println("File is locked");
                File f = new File(katalog+nazwa) ;
                f.delete();
            }
        }finally{
            if (fileLock != null){
                fileLock.release();
            }
        }
        if(!f.exists())
        	return true ;
        else 
        	return false ;
    }
 
    private String czytaj(String nazwa){
 
    	String data = new String() ;
        try{
            FileInputStream input = new FileInputStream(nazwa);
            DataInputStream in = new DataInputStream(input);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            data  = br.readLine();
            System.out.println(data);
        }catch (Exception exception){
            exception.printStackTrace();
            data = new String("ERROR") ;
        }
        
        return data ;
    }
    
    private boolean pisz(String nazwa,String data){
    	 
        try{
            FileOutputStream output = new FileOutputStream(nazwa) ;
            DataOutputStream out = new DataOutputStream(output) ;
            out.writeBytes(data) ;
            return true ;
            
        }catch (Exception exception){
            exception.printStackTrace();
            return false ;
        }
    }
}